package com.sunnada.edu.system.pub.controller;

import java.util.Enumeration;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.sunnada.edu.system.pub.common.BasicController;
import com.sunnada.edu.system.pub.util.Utils;

@Controller
@RequestMapping("/pageJumpController")
public class PageJumpController extends BasicController {

	/**
	 * 页面跳转方法 注意事项：改方法目前暂时支持"两层"的页面跳转
	 * 如前端请求：/pageJumpController/system_map_qqmap.shtml 则跳转为system/map/qqmap
	 * 跳转规则以“_”对应“/”
	 * 
	 * @return
	 */
	@RequestMapping("/{action}")
	public String pageJump(@PathVariable String action, HttpServletResponse response, HttpServletRequest request) {
		try {
			HashMap<String, Object> map = new HashMap<String, Object>();
			Enumeration enr = request.getParameterNames();
			while (enr.hasMoreElements()) {
				String key = String.valueOf(enr.nextElement());
				map.put(key, request.getParameter(key));
			}
			request.setAttribute("map", map);
			request.setAttribute("ctp", request.getContextPath());
			String[] actions = action.split("_");
			String page = "";
			if (Utils.isNotNull(actions[0]) && Utils.isNull(actions[1])) {
				page = actions[0] + "/" + action.substring(actions[0].length() + 1);
			} else {
				page = actions[0] + "/" + actions[1] + "/" + action.substring(actions[0].length() + actions[1].length() + 2);
			}
			return page;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}
